const app = getApp();
Page({

  data: {
    flag:0,
    token:'',
    userInfo:{},
    pro:'',
    showPro:false
  },

  onLoad: function (options) {

  },

  onShow: function () {
    this.getInfo();
  },
  getInfo() { //获取首页数据
    var that = this;
    app.util.request({
      url: app.do('user/service'),
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {},
      success: function (res) {
        if (res.data.code == "0000") {
          console.log(res)
          that.setData({
            pro: res.data.data.activity.service,
          })
        } else if (res.data.code == "2001") {
          wx.navigateTo({
            url: '../auth/auth',
          })
        }
        // that.yao();
      },
      fail: function (res) {
        var text = res.data.message;
        wx.showToast({
          title: text,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },

  show(){
    this.setData({
      showPro:true
    })
  },
  hide(){
    this.setData({
      showPro:false
    })
  },

  updateUserInfo: function (e) {
    var that = this;
    app.util.getUserInfo(function (userInfo) {
      that.setData({
        userInfo: userInfo
      })
      that.member();
    })
  },

  member: function () {
    var that = this;
    var openid = wx.getStorageSync('openid');
    let info = that.data.userInfo;
    app.util.request({
      url: app.do('user/WechatMiniAuth') ,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        m: app.siteInfo.m,
        openid: openid,
        avatar: info.wxInfo.avatarUrl,
        nickname:info.wxInfo.nickName,
        // avatar: info.memberInfo.headimgurl,
        // nickname:info.memberInfo.nickName,
      },
      success: function(res) {
        wx.setStorageSync('token', res.data.data.token);
        that.setData({
          flag:1,
          token:res.data.data.token
        })
        // wx.switchTab({
        //   url:'../home/home'
        // })
      },
      fail: function(res) {
      }
    })
  },
  getPhoneNumber: function(e) { //获取手机号
    var that = this;
    // var user_id = wx.getStorageSync('user/getMobile');
    wx.login({
      success: function(res) { //调用login获取code并将加密的手机号传到后台
        app.util.request({
          url: app.do('user/getMobile'),
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            m: app.siteInfo.m,
            code: res.code,
            mobile: e.detail.encryptedData,
            iv: e.detail.iv,
            token: that.data.token
          },
          success: function(res) {
            if (res.data.code == '0000') {
              wx.switchTab({
                url: '../home/home',
              })
              // // wx.navigateBack({});
              // if (that.data.store_id) {
              //   if (that.data.flag == 'store') { //商家
              //     wx.reLaunch({
              //       url: '../storeDetail/storeDetail?store_id=' + that.data.store_id,
              //     })
              //   } else if (that.data.flag == 'fish') { //钓场
              //     wx.reLaunch({
              //       url: '../fishIntroduce/fishIntroduce?store_id=' + that.data.store_id,
              //     })
              //   }
              // } else if (that.data.flag == 'tuan') { //拼团
              //   wx.reLaunch({
              //     url: '../../tableFishing/indexDetail/indexDetail?id=' + that.data.id,
              //   })
              // } else if (that.data.flag == 'goodsDetail') { //商品详情页
              //   wx.reLaunch({
              //     url: '/pages/index/goodsDetail/goodsDetail?shop_id=' + that.data.shop_id + ' &shoptype=' + that.data.shoptype + '&type=' + that.data.type
              //   })
              // } else if (that.data.flag == 'fishquan') { //渔圈正文
              //   wx.reLaunch({
              //     url: '/pages/index/fishDetail/fishDetail?fish_id=' + that.data.fish_id
              //   })
              // } else if (that.data.flag == 'hisdeve') { //个人主页
              //   wx.reLaunch({
              //     url: '/pages/index/hisDevelop/hisDevelop?is_user_id=' + that.data.is_user_id
              //   })
              // } else if (that.data.flag == 'skillDetail') { //钓技正文
              //   wx.reLaunch({
              //     url: '/pages/index/fishSkillDetail/fishSkillDetail?skill_id=' + that.data.skill_id
              //   })
              // } else if (that.data.flag == 'skilllist') { //钓技正文
              //   wx.reLaunch({
              //     url: '/pages/index/fishSkill/fishSkill'
              //   })
              // } else if (that.data.flag == 'yuding'){
              //   wx.reLaunch({
              //     url: '/pages/index/yuding/yuding?shopmeal_id=' + that.data.shopmeal_id
              //   })
              // } else {
              //   wx.reLaunch({
              //     url: '../../tableFishing/index/index',
              //   })
              // }
            } else {

              var text = res.data.message;
              wx.showToast({
                title: text,
                icon: 'none',
                duration: 2000
              })
            }
          },
          fail: function() { //获取失败提示
            wx.showToast({
              title: '授权失败请重试',
              icon: 'none',
              duration: 2000
            })
          },
          complete: function() {}
        });
      }
    })

  },

  onPullDownRefresh: function () {

  },

  onReachBottom: function () {

  },

  onShareAppMessage: function () {

  }
})